/**
 * Copyright (c) 2008,中企动力华南研发中心
 * All rights reserved.
 */

package com.ce.pms.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ce.pms.authorization.filter.PmsAuthenticateFilter;
import com.ce.pms.authorization.model.AuthorizedInfo;
import com.ce.pms.common.service.IOrganizationalStructureService;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.common.vo.OrganizationalStructureVO;
import com.ce.util.BlankUtil;
import com.ce.util.CurrentUserThread;
import com.ce.webframework.sec.vo.SecUserinfoVO;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.web.i18n.ExceptionMessageLocale;
import com.sitechasia.webx.core.web.struts1.action.BaseStrutsAction;

/**
 * 逻辑、功能相关描述:
 *
 * @author 易贤建 编写日期：2008-1-24
 */

public class PmsBaseAction extends BaseStrutsAction {

	AuthorizedInfo authInfo;

	// 当前登陆用户vo
	OrganizationalStructureVO loginUser;

    protected IOrganizationalStructureService organizationalStructureService;

	public void setOrganizationalStructureService(
			IOrganizationalStructureService organizationalStructureService) {
		this.organizationalStructureService = organizationalStructureService;
	}


	@Override
	protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name)
        throws Exception
    {
		CurrentUserThread.set((OrganizationalStructureVO)request.getSession().getAttribute(PmsAuthenticateFilter.LOGIN_USER_KEY));
		return super.dispatchMethod(mapping,form,request,response,name);

    }

	/**
	 * Function:获取登录用户信息
	 *
	 * @param
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	public OrganizationalStructureVO getLoginUser() {
		return getLoginUser(getRequest());
	}

	/**
	 * Function:获取登录用户信息
	 *
	 * @param
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	public OrganizationalStructureVO getLoginUser(HttpServletRequest request) {
		return (OrganizationalStructureVO) request.getSession().getAttribute(PmsAuthenticateFilter.LOGIN_USER_KEY);
	}

	/**
	 * Function:获取登录用户权限相关信息
	 *
	 * @param
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	public AuthorizedInfo getAuthInfo() {

		return (AuthorizedInfo) this.getRequest().getSession().getAttribute(PmsAuthenticateFilter.LOGIN_USER_RIGHT_KEY);
	}

	/**
	 * Function: 初始化管理区，大部分页面独有管理区下拉列表的的需求 根据第一次迭代，有很多地方
	 * 都需要初始化管理区 编码为开始，先放个空方法
	 *
	 * @param
	 * @return Create author:易贤建 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	protected void initArea() {

	}

	/**
	 * Function:从国际化配置文件读取key对应的消息
	 * 该方法主要用于后台验证,所有后台验证消息配置在i18n\pms\exception\common.properties文件中,
	 * 其实现是用Struts的消息国际化机制，在服务启动时加载了配置文件---struts.xml配置.
	 *
	 * @param key
	 *            在common.properties配置的对应的key
	 * @return Create author:易贤建 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	protected String getMessage(String key) {
		return this.getResources(this.getRequest()).getMessage(key);
	}


	/**
	 * Function:得到userId
	 *
	 * @param
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	protected Long getUserId() {
		OrganizationalStructureVO user = this.getLoginUser();
		if (user != null) {
			return user.getUserId();
		}
		return null;
	}

	/**
	 * Function:从session取值
	 *
	 * @param key
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	protected Object getAtrributeFromSeesion(String key) {
		return getRequest().getSession().getAttribute(key);
	}

	/**
	 * Function:从session取值
	 *
	 * @param key
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	protected void removeSessionAttribute(String attribute) {
		String temp = (String) this.getRequest().getParameter("attribute");
		if (temp != null) {
			attribute = temp;
		}
		this.getRequest().getSession().removeAttribute(attribute);
	}

	/**
	 * Function:得到SecUserinfoVO
	 *
	 * @param key
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	protected OrganizationalStructureVO getSecUserinfoVO() {
		// Object obj = SecurityContextHolder.getContext().getAuthentication()
		// .getPrincipal();
		OrganizationalStructureVO user = this.getLoginUser();
		if (user != null) {
			return user;
		}
		return null;
	}

	/**
	 * Function:得到username不是登陆名
	 *
	 * @param key
	 * @return
	 * Create author:刘科学
	 * Create on:2008-1-24
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected String getUsername() {
		// Object obj = SecurityContextHolder.getContext().getAuthentication()
		// .getPrincipal();
		OrganizationalStructureVO user = this.getLoginUser();
		if (user != null) {
			return user.getEmployeeName();
		}
		return null;
	}

	/**
	 * Function:得到epId
	 *
	 * @param key
	 * @return Create author:刘科学 Create on:2008-1-24 Edit author: Edit on: Why:
	 */
	protected String getEpId() {
		// Object obj = SecurityContextHolder.getContext().getAuthentication()
		// .getPrincipal();
		OrganizationalStructureVO user = this.getLoginUser();
		if (user != null) {
			return user.getDept();
		}
		return null;
	}

	/**
	 * 系统参数表服务类接口注入
	 */
	protected ISysrefcodeService sysrefcodeService;

	/**
	 * 功能描述：获取系统参数表服务类接口
	 *
	 * @author 付湘辉
	 * Created on 2008-05-30
	 *
	 * @return 系统参数表服务类接口
	 */
	public ISysrefcodeService getSysrefcodeService() {
		return sysrefcodeService;
	}

	/**
	 * 功能描述：获取系统参数表服务类接口
	 *
	 * @author 付湘辉
	 * Created on 2008-05-30
	 *
	 * @return 系统参数表服务类接口
	 */
	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService) {
		this.sysrefcodeService = sysrefcodeService;
	}

	/**
	 * Function:
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String getLocaleMessage(String key){

		ExceptionMessageLocale localMessage = new ExceptionMessageLocale(this.getRequest());
		String fileName="";
		String returnMessage="";

		if(key.indexOf(":")!=-1){
			fileName=key.substring(0, key.indexOf(":"));
			key=key.substring(key.indexOf(":")+1, key.length());
		}

		returnMessage=localMessage.getLocaleMessage(fileName, key);

		if(BlankUtil.isBlank(returnMessage)){
			 returnMessage="没有找到相应的提示信息";
		}


		return returnMessage;
	}

	/**
	 * Function:
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void getAjaxMessage(@Read(key="key")String key){
		if(!BlankUtil.isBlank(key)){

		ExceptionMessageLocale localMessage = new ExceptionMessageLocale(this.getRequest());
		key.indexOf(":");

		String fileName="";

		if(key.indexOf(":")!=-1){
			fileName=key.substring(0, key.indexOf(":"));
			key=key.substring(key.indexOf(":")+1, key.length());
		}

		this.renderText(localMessage.getLocaleMessage(fileName, key));
		} else {
			this.renderText("");
		}

	}

}
